<?php

	// +----------------------------------------------------------------------
	// | 友情链接
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\cms\admin;

	use app\model\cms\FriendLink as FriendLinkModel;
	use app\event\cms\FriendLink as FriendLinkEvent;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use app\controller\admin\Common;

	class FriendLink extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		/**
		 * 友情链接列表展示
		 * @return string
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2019/6/24 19:37
		 */
		public function index()
		{
			// 显示页面
			$builder = new Table();
			$builder->model(FriendLinkModel::class)
				->suggest('请增加优质的友情链接，低质的网站会影响本身网站的收录')
				->buttonUpdate()
				->keyId()
				->keyTitle()
				->keyText('url', 'url地址')
				->keyText('sort', '排序')
				->keyImageModel('image|image_id', 'LOGO')
				->keyText('description', '描述')
				->keyUpdateTime()
				//                ->keyStatus()
				->actionUpdate()
				->actionForbid()
				->actionRestore()
				->actionDelete();
			return $builder->fetch();
		}

		/**
		 * 增加修改友情链接
		 * @return string|void
		 * @throws \Exception
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2019/6/24 19:37
		 */
		public function update()
		{
			if ($this->request->isPost()) {
				parent::_update(FriendLinkEvent::class);
			} else {
				$id = $this->request->get('id');
				// 判断是否为编辑模式
				$isEdit = $id ? true : false;
				$builder = new Edit();
				if ($isEdit) {
					$data = FriendLinkModel::where('id', $id)->find();
				} else {
					$data = [];
				}
				$builder->title($isEdit ? '编辑友情链接' : '添加友情链接')
					->keyTextInline('title', '名称')
					->keyUrl('url', 'url地址', '')
					->keyBool('time_switch', '显示控制')
					->keyDateTimeRange('start_time|end_time', '有效时间')
					//                    ->keyRadio('type', '链接类型', ['图片', '文字'])
                    ->keyImageModel('image_id', 'LOGO', '')
					->keySort('sort', '排序', '数值越小越靠前，最大255', 0)
					->keyRadio('location', '显示位置', ['不限', '主页', '内页'], '')
					->keyTextArea('description', '描述', '可为空')
					->setTrigger('time_switch', '0', '')
					->setTrigger('time_switch', '1', 'start_time|end_time')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}
	}
